/**
 * 作者：陈文斌 on 2019/1/12.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/model/Company/company_category_model.dart';
import 'package:crm/viewModel/ClientManager/industry_category_view_model.dart';
import 'package:crm/model/main_scoped_model.dart';
import 'package:crm/component/IndustryCategory/industry_category_list_row_section.dart';
import 'package:crm/component/IndustryCategory/industry_sub_category_list_row_section.dart';

class IndustryCategoryListSection extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _IndustryCategoryListSectionState();
}

class _IndustryCategoryListSectionState
    extends State<IndustryCategoryListSection> {
  List<CompanyCategoryInfoModel> _industryCategories =
      List<CompanyCategoryInfoModel>();

  bool _isPerformingRequest = false;
  int _page = 0;

  // 选中的IndustryCategory，默认为：0
  int _selIndustryCategoryIndex = 0;

  _get(int page) async {
    if (!_isPerformingRequest) {
      _isPerformingRequest = true;

      IndustryCategoryVM().getIndustryCategories(
        page,
        _industryCategories,
        callback: (industries) {
          // 请求成功
          _isPerformingRequest = false;
          _industryCategories = industries;
          if (!mounted) return;
          setState(() {});
        },
        errorCallback: (errMsg) => print(errMsg),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    _selIndustryCategoryIndex =
        MainModel().of(context).selIndustryCategoryIndex;
    _get(_page);
  }

  @override
  Widget build(BuildContext context) {
    MainModel industryCategorySM = MainModel().of(context);
    return Expanded(
      child: Row(
        children: <Widget>[
          Container(
            width: AppScreen.setWidth(90),
            child: ListView.builder(
              itemCount: _industryCategories.length,
              itemBuilder: (context, i) {
                CompanyCategoryInfoModel _industryCategory =
                    _industryCategories[i];
                return IndustryCategoryListRow(
                  title: _industryCategory.Name ?? '',
                  isSelect: _selIndustryCategoryIndex == i,
                  onTap: () {
                    setState(() {
                      _selIndustryCategoryIndex = i;
                    });
                  },
                );
              },
            ),
          ),
          Container(
            width: AppScreen.setWidth(375 - 90),
            color: Colors.white,
            padding: EdgeInsets.all(10.0),
            child: ListView.builder(
              itemCount: _industryCategories.length > 0 ? 1 : 0,
              itemBuilder: (context, i) {
                final CompanyCategoryInfoModel _industryCategory =
                    _industryCategories[_selIndustryCategoryIndex];

                return Wrap(
                  spacing: 10,
                  runSpacing: 5,
                  children: _industryCategory.SubCategories.map(
                      (CompanyCategoryInfoModel subCategory) {
                    final bool isSelect =
                        industryCategorySM.saveIndustryCategory.Id ==
                            subCategory.Id;
                    return IndustrySubCategoryListRowSection(
                      title: subCategory.Name,
                      isSelect: isSelect,
                      onTap: () {
                        setState(() {
                          industryCategorySM.saveIndustryCategoryAt(
                            _selIndustryCategoryIndex,
                            subCategory,
                          );
                          industryCategorySM.client.industryClassification = '${subCategory.Name ?? ''}';
                        });
                      },
                    );
                  }).toList(),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
